package com.hongning.railway.app;

import android.annotation.SuppressLint;
import android.app.Application;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProvider;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;

import com.hongning.railway.data.AppRepository;
import com.hongning.railway.ui.categorylistnews.CategoryListNewsFragmentViewModel;
import com.hongning.railway.ui.categorylistnews.NewDetailFragmentViewModel;
import com.hongning.railway.ui.dynamic_photo.DynamicPhotoFragmentViewModel;
import com.hongning.railway.ui.login.LoginViewModel;
import com.hongning.railway.ui.main.cadremanager.CadreGradeFragmentViewModel;
import com.hongning.railway.ui.main.cadremanager.CadreManagerFragmentViewModel;
import com.hongning.railway.ui.main.fragments.MineViewModel;
import com.hongning.railway.ui.main.fragments.cadremanagement.CadreAssessmentViewModel;
import com.hongning.railway.ui.main.fragments.cadremanagement.CadreManagementRosterListViewModel;
import com.hongning.railway.ui.main.fragments.cadremanagement.CadreReslutFragmentViewModel;
import com.hongning.railway.ui.main.fragments.cadremanagement.viewmodel.CadreManagementRosterInfoViewModel;
import com.hongning.railway.ui.main.fragments.college.CollegeFilterViewModel;
import com.hongning.railway.ui.main.fragments.college.ExamCompleteFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.ExamUnCompleteFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.LearnCompleteFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.LearnUnCompleteFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.exam.CheckAnswerFragmentViewModel;
import com.hongning.railway.ui.main.fragments.college.exam.ExamDetailFragmentViewModel;
import com.hongning.railway.ui.main.fragments.dynamic.DynamicViewModel;
import com.hongning.railway.ui.main.fragments.home.HomeViewModel;
import com.hongning.railway.ui.main.fragments.threesessionsandonelesson.ThreesessionSanDoneLessonInfoViewModel;
import com.hongning.railway.ui.main.fragments.threesessionsandonelesson.ThreesessionSanDoneLessonMsgViewModel;
import com.hongning.railway.ui.main.fragments.threesessionsandonelesson.ThreesessionSanDoneLessonViewModel;
import com.hongning.railway.ui.main.mine.PartyMemberInfoViewModel;
import com.hongning.railway.ui.main.mine.experience.ExperienceFragmentViewModel;
import com.hongning.railway.ui.main.mine.experience.ExperiencePubOrEditFragmentViewModel;
import com.hongning.railway.ui.main.mine.partcheck.PartCheckMainFragmentViewModel;
import com.hongning.railway.ui.main.mine.partcheck.PartCheckSecondFragmentViewModel;
import com.hongning.railway.ui.main.mine.partcheck.PartyCheckDetailFragmentViewModel;
import com.hongning.railway.ui.main.mine.partymember.CheckReultFragmentViewModel;
import com.hongning.railway.ui.main.mine.partymember.PartyQuestionFragmentViewModel;
import com.hongning.railway.ui.main.mine.partymember.QuestionFragmentViewModel;
import com.hongning.railway.ui.main.mine.score.MainScoreFragmentViewModel;
import com.hongning.railway.ui.main.mine.score.RankTypeFragmentViewModel;
import com.hongning.railway.ui.main.mine.score.SocreDetailFragmentViewModel;


/**
 * Created by goldze on 2019/3/26.
 */
public class AppViewModelFactory extends ViewModelProvider.NewInstanceFactory {
    @SuppressLint("StaticFieldLeak")
    private static volatile AppViewModelFactory INSTANCE;
    private final Application mApplication;
    private final AppRepository mRepository;

    public AppRepository getmRepository() {
        return mRepository;
    }

    public static AppViewModelFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (AppViewModelFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
                }
            }
        }
        return INSTANCE;
    }

    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    private AppViewModelFactory(Application application, AppRepository repository) {
        this.mApplication = application;
        this.mRepository = repository;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel(mApplication, mRepository);
        }if (modelClass.isAssignableFrom(HomeViewModel.class)){
            return (T) new HomeViewModel(mApplication, mRepository);
        }else if (modelClass.isAssignableFrom(CategoryListNewsFragmentViewModel.class)){
            return (T) new CategoryListNewsFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(DynamicViewModel.class)){
            return (T) new DynamicViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(NewDetailFragmentViewModel.class)){
            return (T) new NewDetailFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(DynamicPhotoFragmentViewModel.class)){
            return (T) new DynamicPhotoFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(LearnCompleteFragmentViewModel.class)){
            return (T) new LearnCompleteFragmentViewModel(mApplication,mRepository);
        } else if (modelClass.isAssignableFrom(LearnUnCompleteFragmentViewModel.class)){
            return (T) new LearnUnCompleteFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ExamDetailFragmentViewModel.class)){
            return (T) new ExamDetailFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(PartyMemberInfoViewModel.class)){
            return (T) new PartyMemberInfoViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ExamUnCompleteFragmentViewModel.class)){
            return (T) new ExamUnCompleteFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ExamCompleteFragmentViewModel.class)){
            return (T) new ExamCompleteFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ThreesessionSanDoneLessonViewModel.class)){
            return (T) new ThreesessionSanDoneLessonViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(CheckAnswerFragmentViewModel.class)){
            return (T) new CheckAnswerFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ThreesessionSanDoneLessonInfoViewModel.class)){
            return (T) new ThreesessionSanDoneLessonInfoViewModel(mApplication,mRepository);
        } else if (modelClass.isAssignableFrom(CadreGradeFragmentViewModel.class)){
            return (T) new CadreGradeFragmentViewModel(mApplication,mRepository);
        } else if (modelClass.isAssignableFrom(CadreManagerFragmentViewModel.class)){
            return (T) new CadreManagerFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ThreesessionSanDoneLessonMsgViewModel.class)){
            return (T) new ThreesessionSanDoneLessonMsgViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(CadreManagementRosterListViewModel.class)){
            return (T) new CadreManagementRosterListViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(MainScoreFragmentViewModel.class)){
            return (T) new MainScoreFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(SocreDetailFragmentViewModel.class)){
            return (T) new SocreDetailFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(RankTypeFragmentViewModel.class)){
            return (T) new RankTypeFragmentViewModel(mApplication,mRepository);
        } else if (modelClass.isAssignableFrom(PartCheckMainFragmentViewModel.class)){
            return (T) new PartCheckMainFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(PartCheckSecondFragmentViewModel.class)){
            return (T) new PartCheckSecondFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(PartyCheckDetailFragmentViewModel.class)){
            return (T) new PartyCheckDetailFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ExperienceFragmentViewModel.class)){
            return (T) new ExperienceFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(ExperiencePubOrEditFragmentViewModel.class)){
            return (T) new ExperiencePubOrEditFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(CadreManagementRosterInfoViewModel.class)){
            return (T) new CadreManagementRosterInfoViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(QuestionFragmentViewModel.class)){
            return (T) new QuestionFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(CheckReultFragmentViewModel.class)){
            return (T) new CheckReultFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(MineViewModel.class)){
            return (T) new MineViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(CadreAssessmentViewModel.class)){
            return (T) new CadreAssessmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(CadreReslutFragmentViewModel.class)){
            return (T) new CadreReslutFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(PartyQuestionFragmentViewModel.class)){
            return (T) new PartyQuestionFragmentViewModel(mApplication,mRepository);
        }else if (modelClass.isAssignableFrom(CollegeFilterViewModel.class)){
            return (T) new CollegeFilterViewModel(mApplication,mRepository);
        }

        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
